package com.ldc.common.utils;

import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;

import com.ldc.common.exception.CommonException;
import com.ldc.common.mvvm.ViewModelBase;
import com.ldc.common.mvvm.ViewModelFactoryBase;

public class ViewModelUtils {


    public ViewModelUtils() {
        throw new CommonException("非法实例化");
    }

    /**
     * 获取实例化
     *
     * @param owner owner
     * @param clazz 类型
     * @param <T>   泛
     * @return viewmodel
     */
    public static <T extends ViewModelBase> T getViewModel(ViewModelStoreOwner owner, Class<T> clazz) {
        return getViewModel(owner, clazz, new ViewModelFactoryBase());
    }

    public static <T extends ViewModelBase> T getViewModel(ViewModelStoreOwner owner, Class<T> clazz, ViewModelFactoryBase factory) {
        ViewModelProvider viewModelProvider = new ViewModelProvider(owner, factory);
        return viewModelProvider.get(clazz);
    }
}
